<template>
  <div class="block">

<!--  页面数据-->
    <el-table
      :data="userList.filter(data => !search || data.name.toLowerCase().includes(search.toLowerCase()))"
      style="width: 100%">
      <el-table-column
        label="名字"
        prop="name">
      </el-table-column>
      <el-table-column
        label="性别"
        prop="sex">
      </el-table-column>
      <el-table-column
        label="出生日期"
        prop="birthday">
      </el-table-column>
      <el-table-column
        label="公司"
        prop="company">
      </el-table-column>
      <el-table-column
        label="职位"
        prop="position">
      </el-table-column>
      <el-table-column
        label="手机"
        prop="mobile">
      </el-table-column>
      <el-table-column
        label="浏览量"
        prop="visitied">
      </el-table-column>
      <el-table-column
        align="right">
        <template slot="header" slot-scope="scope">
          <el-input
            v-model="search"
            size="mini"
            placeholder="输入关键字搜索"/>
        </template>
        <template slot-scope="scope">
          <el-button
            size="mini"
            :type="scope.row.type"
            @click="handleEdit(scope.$index, scope.row)" >喜欢</el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)" >详情</el-button>
        </template>
      </el-table-column>
    </el-table>

<!--    详情框-->
    <el-dialog title="收货地址" :visible.sync="dialogFormVisible" style="margin-top: -20px">
      <el-form :model="handle">
        <el-form-item label="名字" :label-width="formLabelWidth" >
          <el-input v-model="handle.name" autocomplete="off" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="性别" :label-width="formLabelWidth">
          <el-radio-group v-model="handle.sex" :disabled="true">
            <el-radio label="男"></el-radio>
            <el-radio label="女"></el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="出生日期" :label-width="formLabelWidth">
            <el-date-picker type="date" placeholder="选择日期" v-model="handle.birthday" style="width: 100%;" :disabled="true"></el-date-picker>
        </el-form-item>

        <el-form-item label="公司" :label-width="formLabelWidth">
          <el-input v-model="handle.company" autocomplete="off" :disabled="true"></el-input>
        </el-form-item>

        <el-form-item label="职位" :label-width="formLabelWidth">
          <el-input v-model="handle.position" autocomplete="off" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="手机" :label-width="formLabelWidth">
          <el-input v-model="handle.mobile" autocomplete="off" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="浏览量" :label-width="formLabelWidth">
          <el-input v-model="handle.visitied" autocomplete="off" :disabled="true"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogFormVisible = false">确 定</el-button>
      </div>
    </el-dialog>
<!--    分页-->
    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageInfo.pageNum"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="pageInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pageInfo.total">
      </el-pagination>
    </div>



  </div>


</template>

<script>

export default {
  name: "Home",
  data(){
    return{
      user : this.$store.state.user,
      //用户数据
      userList:[],
      //分页数据
      pageInfo:{
        pageNum:1,
        pageSize:5
      },
      search: '',
      dialogFormVisible: false,
      formLabelWidth: '120px',
      handle:{}
    }
  },
  mounted() {
    this.showAll();
  },
  methods: {
    //查询全部
    showAll(){
      let vm = this;
      this.axios({
        url:`/localhost/crad/showAll?pageNum=${vm.pageInfo.pageNum}&pageSize=${vm.pageInfo.pageSize}`,
        method:"get"
      }).then(function (res) {
        let list = res.data.data.list;
        list.forEach(crad =>{
          console.log(crad)
          if (vm.user.liked.indexOf(crad.id) === -1){
            crad.type = '';

          }else {
            crad.type = 'danger';
          }
        })
        // console.log(res.data.data);
        vm.pageInfo = res.data.data;
        vm.userList = list;
      }).catch(function (err) {
        console.log(err)
      });
    },
    //喜欢
    handleEdit(index, row) {
      //vmCount
      console.log(row);
      let vm = this;
      let list = vm.user.liked.indexOf(row.id) === -1;
        this.axios({
          url: `/localhost/user/like?userId=${vm.user.id}&card=${row.id}&like=${list}`,
          method:'get'
        }).then(function (res) {
          console.log(res.data);
          vm.user = res.data.data;
          vm.$store.commit('UpdateUser',res.data.data);
          vm.showAll();
        }).catch(function (err) {
          console.log(err)
        });
    },
    //查看详情信息
    handleDelete(index, row) {
      this.dialogFormVisible = true;
      console.log(index, row);
      let vm = this;
      this.axios({
        url:`/localhost/crad/browse?id=${row.id}`,
        method:'get'
      }).then(function (res) {
        console.log(res.data.data);
        vm.handle = res.data.data;
        vm.showAll();
      }).catch(function (err) {
        console.log(err)
      });
    },
    //分页
    handleSizeChange(val) {
      this.pageInfo.pageSize = val;
      this.showAll();
    },
    handleCurrentChange(val) {
      this.pageInfo.pageNum = val;
      this.showAll();
    },
  },

}
</script>

<style scoped>
  .block{
    text-align: center;
  }
</style>
